package fr.lip6.move.care.sdmaker.validation;

import java.util.ArrayList;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;

import fr.lip6.move.care.sdmaker.sDMaker.Declaration;
import fr.lip6.move.care.sdmaker.sDMaker.Diagram;
import fr.lip6.move.care.sdmaker.sDMaker.Element;
import fr.lip6.move.care.sdmaker.sDMaker.Lifeline;
import fr.lip6.move.care.sdmaker.sDMaker.SDMakerModel;

public class SDMakerValidatorUtils {

	public static ArrayList<String> getAllActorsNames(Diagram d){
		ArrayList<String> actorsNames= new ArrayList<String>();

		for(Element e : d.getElements()){
			if(e instanceof Lifeline){
				Lifeline l= (Lifeline)e;
				if(l.getKind().compareTo("Actor")==0){
					actorsNames.add(l.getName());
				}
			}
		}

		return actorsNames;
	}

	public static ArrayList<String> getAllObjectsNames(Diagram d){
		ArrayList<String> objectsNames= new ArrayList<String>();

		for(Element e : d.getElements()){
			if(e instanceof Lifeline){

				Lifeline l= (Lifeline)e;
				if(l.getKind().compareTo("Object")==0){
					objectsNames.add(l.getName());
				}
			}
		}

		return objectsNames;
	}

	public static ArrayList<Declaration> getAllDeclarations(SDMakerModel model){
		ArrayList<Declaration> allDeclaration= new ArrayList<Declaration>();
		TreeIterator<EObject> allContent= model.eAllContents();

		while(allContent.hasNext()){
			EObject tmp= allContent.next();
			if(tmp instanceof Declaration){
				allDeclaration.add((Declaration)tmp);
			}
		}

		return allDeclaration;
	}

	public static ArrayList<String> eraseMultipleSameString(ArrayList<String> errors){
		Boolean haveDoublon= false;

		do{
			haveDoublon= false;
			for(int i=0; (i < errors.size()) && haveDoublon==false; i++){
				for(int j=0; (j < errors.size()) && haveDoublon==false; j++){
					if(i != j){
						if(errors.get(i).compareTo(errors.get(j))==0){
							errors.remove(i);
							haveDoublon=true;
						}
					}
				}
			}
		}while(haveDoublon==true);

		return errors;
	}

	public static String declarationToString(Declaration d){
		String toString= d.getTypeOwner()+":"+d.getName()+"(";

		for(int i= 0; i < (d.getParams().size()-1); i++){
			toString+=d.getParams()
					.get(i)
					.getParameterType()
					.getSimpleName()+", ";
		}

		if(d.getParams().size() > 0){
			toString+=d.getParams()
					.get(d.getParams().size()-1)
					.getParameterType()
					.getSimpleName();
		}

		toString+=")";

		return toString;
	}
}